package userInterfaces;

import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import managerBean.RegistrazioneManagerBean;

import org.apache.commons.codec.digest.DigestUtils;

@ManagedBean(name="RegistrazioneManagedBean")
@ViewScoped
public class RegistrazioneManagedBean {

	@EJB
	private RegistrazioneManagerBean regMgrBean;
	
	private String username;
	
	private Date dataNascita2;
	
	public Date getDataNascita2() {
		return dataNascita2;
	}

	public void setDataNascita2(Date dataNascita2) {
		this.dataNascita2 = dataNascita2;
	}

	private String email;

	private int idCarrello;
	
	private int idWishlist;
	
	private String indirizzo;
	
	private String password;
	
	private Date currentDate = new Date();


	@SuppressWarnings("deprecation")
	public Date getCurrentDate() {
		int year=currentDate.getYear()-18;
		currentDate.setYear(year);
		return currentDate;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public RegistrazioneManagedBean() {
		super();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getIdCarrello() {
		return idCarrello;
	}

	public void setIdCarrello(int idCarrello) {
		this.idCarrello = idCarrello;
	}

	public int getIdWishlist() {
		return idWishlist;
	}

	public void setIdWishlist(int idWishlist) {
		this.idWishlist = idWishlist;
	}

	public String getIndirizzo() {
		return indirizzo;
	}

	public void setIndirizzo(String indirizzo) {
		this.indirizzo = indirizzo;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public void registraUtente() throws ParseException{
		String crypto=DigestUtils.sha1Hex(this.password);
		regMgrBean.registraUtenteMgr(this.username,crypto,this.email,this.indirizzo,this.dataNascita2);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("./home.xhtml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
